//FlyweightFactory.cpp
#include "flyweight_factory.h"
#include <iostream>
#include <string>
#include <cassert>

FlyweightFactory::FlyweightFactory()
{
}

FlyweightFactory::~FlyweightFactory()
{
    for (auto it = _fly.begin(); it != _fly.end(); it++)
    {
        delete *it;
    }
}

Flyweight* FlyweightFactory::GetFlyweight(const std::string& key)
{
    auto it = _fly.begin();
    for (; it != _fly.end();it++)
    {
        //找到了，就一起用，^_^
        if ((*it)->GetIntrinsicState() == key)
        {
            std::cout << "already created by users...." << std::endl;
            return *it;
        }
    }

    Flyweight* fn = new ConcreteFlyweight(key);
    _fly.push_back(fn);
    return fn;
}

